<?php session_start(); ?>
<?php
include('../ketnoi.php');
include_once("fckeditor/fckeditor.php") ;
if (!$_SESSION['user']) header("location: index.php"); // nếu biến session['banguyen'] chưa có thì trở về trang đăng nhập, copy dòng này vào trang nào muốn bảo vệ bằng mật khẩu
?>
<style type="text/css">
<!--
.error, .notice, .success {padding:.8em;margin:auto; margin-bottom:.5em;border:2px solid #AAF;width:570px;text-align: center}
.error {background: #ECECFF;color: red;border-color:#AAF;}
-->
</style>
<style type="text/css">
.xem {
FONT-SIZE: 10pt; 
	COLOR: #000000;
	BORDER:0px;
	background-color:#FFFFFF;
}
</style>
<script type="text/javascript" src="scripts/joomla.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script src="ckeditor/_samples/sample.js" type="text/javascript"></script>
<div class="m">
 <?php 
    $shop=$dulieu;
	if($_GET['action']=='view' && $_GET['option']=='com_advertisement')
	{
		$sql1 = "SELECT * FROM `".$shop."`.`coso` order by ViTri asc";
		$ketqua = mysql_query($sql1) or die ('khong tim thay du lieu');
		$tong=mysql_num_rows($ketqua);
		if($_POST['limit']==""){$sotrang = ceil($tong/20);}else{
		$sotrang = ceil($tong/$_POST['limit']);}
	    $dem = -1;
		
	if(isset($_POST['trang'])){
	 $trang = $_POST['trang'];
	}else{	
     if(isset($_POST['end'])){
	  $trang =$sotrang;
	 }else{
	  if(isset($_POST['start'])){
	   $trang =1;
	  }else{
	   $trang=1;
	  }
	 }	
	}
?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="index.php"><img src="images/toolbar/icon-32-cancel.png" alt="Remove"><br><span>Cancel</span></a>
	</div>
	</div>

	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" href="?option=com_advertisement&action=insert"><img src="images/toolbar/icon-32-new.png" alt="New"><br><span>New</span></a>
	</div>
	</div>
</div>

<div class="header" style="background: url(images/dashboard/icon-48-article.png) no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Các cơ sở</h2></div><br /><br />
<form name="adminForm" action="" method="post">
					
					<input name="option" value="com_advertisement" type="hidden">
					<input name="boxchecked" type="hidden">
					<input name="task" value="" type="hidden">
					<input name="func" value="" type="hidden">

<table class="adminlist" width="100%">
<tbody><tr><th width="2%" class="title">#</th>
<th width="3%" class="title"><input name="toggle" value="" onclick="checkAll(<?php if($_POST['limit']==""){echo '20';}else{echo $_POST['limit']; } ?>)" type="checkbox"></th>
<th class="title" width="15%"> 	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tên cơ sở</th>
<th class="title" width="26%"> &nbsp;Địa chỉ</th> 
<th class="title" width="16%"> &nbsp;Email </th> 	 	 	 	 	
<th class="title" width="11%"> &nbsp;Số điện thoại </th>
<th class="title" width="13%"> &nbsp;Vị trí </th> 
<th width="7%" class="title"> &nbsp;Sửa </th>
<th class="title" width="7%"> &nbsp;Xóa</th>
</tr>
 <?php 
	$stt=0;
	$t=0;
   while($dongDL = mysql_fetch_array($ketqua))
	  { 
	  $dem++;
	    if($_POST['limit']==""){$s=20;}
		else{$s=$_POST['limit'];}
		if($dem >= ($trang-1)*$s && $dem <= ($trang*$s)-1)
		{
  ?>
  
<tr class="row<?php echo $t ?>">
<td align="center"><?php echo $dem+1 ?></td>
<td align="center"><input id="cb<?php echo $stt ?>" name="product_id[<?php echo $dongDL['MaCS']?>]" value="<?php echo $dongDL['MaCS']?>" onclick="isChecked(this.checked);" type="checkbox"></td>
<td><?php echo $dongDL['TenCS']?></td>
<td><?php echo $dongDL['DiaChiCS']?></td>
<td><?php echo $dongDL['Email']?></td>
<td ><?php echo $dongDL['SDT']?></td>

<td class="order" align="center">
<?php if( $dongDL['ViTri']!=$tong){ ?>
	<a href="?option=com_advertisement&action=update_dx&amp;id=<?php echo $dongDL['MaCS'] ?>" title="Đi Xuống"><img src="images/downarrow.png" alt="Đi Xuống" width="16" border="0" height="16"></a> <?php }else{ echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' ;} ?>
	<input disabled="disabled" name="order[<?php echo $dongDL['ViTri']?>]" size="4" value="<?php echo $dongDL['ViTri'] ?>" class="text_area" style="text-align: center;" type="text">
<?php if( $dongDL['ViTri']!=1){ ?>
	<a href="?option=com_advertisement&action=update_dl&amp;id=<?php echo $dongDL['MaCS'] ?>" title="Đi Lên"><img src="images/uparrow.png" alt="Đi Lên" width="16" border="0" height="16"></a>  <?php }else{ echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' ;} ?>
</td>

<td align="center"><a href="?option=com_advertisement&action=update&amp;id=<?php echo $dongDL['MaCS'] ?>"><img src="images/edit_f2.png" width="20" height="20" name="Edit<?php echo $stt+1 ?>" alt="Edit" border="0"></a></td>
<td align="center"><a class="toolbar" href="?option=com_advertisement&action=delete&amp;id=<?php echo $dongDL['MaCS'] ?>" onclick="return confirm('Bạn có chắc chắn muốn xóa?');"><img src="images/delete_f2.png" width="20" height="20"alt="Delete" name="delete<?php echo $stt+1 ?>" align="middle" border="0"></a></td>
</tr>
	 <?php
	 $stt=$stt+1;
	 if($t==0)$t=1;
	 else $t=0;
	 }
	  }//ket thuc ham while in du lieu
	  ?>
</tbody></table>

<table class="adminlist"><tbody>
<?php if($sotrang > 1 && $keyword==""){?>
<tr>
  <td colspan="3" align="center"> 
  <input name="start" class="button2" type="submit" value="Start"/>
  <?php
for($i=1; $i<=$sotrang; $i++)
{ ?>
	<input name="trang" class="button" type="submit" value="<?php echo $i;?>"/>
<?php 
}
?>
<input name="end" class="button2" type="submit" value="End"/></td></tr>
<?php } ?>
<?php 
if($keyword=="" && $coso=='' && $trangthai==''){
?>
<tr><td width="48%" align="right" nowrap="nowrap">Display #</td><td>
<select class="inputbox" name="limit" size="1" onchange="this.form.submit();">
<option value="5"  <?php if($_POST['limit']==5) echo "selected='selected'" ?>>5</option>
<option value="10" <?php if($_POST['limit']==10) echo "selected='selected'" ?>>10</option>
<option value="15" <?php if($_POST['limit']==15) echo "selected='selected'" ?>>15</option>
<option value="20" <?php if($_POST['limit']=='') echo "selected='selected'" ?>>20</option>
<option value="25" <?php if($_POST['limit']==25) echo "selected='selected'" ?>>25</option>
<option value="30" <?php if($_POST['limit']==30) echo "selected='selected'" ?>>30</option>
<option value="50" <?php if($_POST['limit']==35) echo "selected='selected'" ?>>50</option>
</select>
</td><td width="48%" align="left" nowrap="nowrap">Results 1 - 
<?php if($_POST['limit']==''){if($tong < 20 ){echo $tong;}else echo '20';} else{if($_POST['limit'] > $tong ){echo $tong;}else echo $_POST['limit'];}?> of <?php echo $tong ?></td></tr>
<?php }else{
?>
<tr><td width="48%" align="right" nowrap="nowrap">Display #</td><td>
<select class="inputbox" name="limit" size="1" disabled="disabled">
<option><?php echo $tong1; ?></option>
</select>
</td><td width="48%" align="left" nowrap="nowrap"><?php if($tong1 >0){?>Results 1 - 
<?php echo $tong1;?> of <?php echo $tong1; }?></td></tr>
<?php
}
?>
</tbody></table>
</form>
<?php
}//ket thuc view

if($_GET['action']=='delete' && $_GET['option']=='com_advertisement')
	{
		  $sql = "Delete from `".$shop."`.`coso` where MaCS = '".$_GET['id']."'";
		  $delete = mysql_query($sql);
          echo"<script language=javascript>window.location='index.php?option=com_advertisement&action=view';</script>";
	  }// ket thuc kiem tra action Delete
	  
 
if($_GET['action']=='insert' && $_GET['option']=='com_advertisement')
{
if(isset($_POST['s1'])){
	$error="";
	if($_POST['s1']=="" || $_POST['s2']=="" || $_POST['s3']=="" || $_POST['s4']==""){
	  $error="Vui lòng nhập đầy đủ các field.";
    }else{ 
        $sql_t = "SELECT * FROM `".$shop."`.`coso`";
		$ketqua_t = mysql_query($sql_t) or die ('khong tim thay du lieu');
		$tong=mysql_num_rows($ketqua_t);
		$tong=$tong+1;
		
	    $sql = "INSERT INTO `".$shop."`.`coso` (`TenCS`,`DiaChiCS`,`Email`,`SDT`,`ViTri`,`TrangThai`) VALUES ('".$_POST['s1']."','".$_POST['s2']."','".$_POST['s3']."','".$_POST['s4']."','".$tong."',1);";
		$insert = mysql_query($sql);
        echo"<script language=javascript>window.location='index.php?option=com_advertisement&action=view';</script>";
	}
}


	?>
	<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="?option=com_advertisement&action=view"><img src="images/toolbar/icon-32-cancel.png" alt="Remove"><br><span>Cancel</span></a>
	</div>
	</div>
	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" onclick="adminForm2.submit();"><img src="images/toolbar/icon-32-save.png"  alt="New"><br><span>Save</span></a>
	</div>
	</div>
</div>

<div class="header" style="background: url(images/dashboard/icon-48-section.png)  no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Cơ Sở: [ Thêm mới ]</h2>
</div><br /><br />
<?php
	if($error!=""){
	?>
	 <div class="error" align="center"><img src='../images/cancel_f2.png' width='10px' height='10px'/> <?php echo $error ?></div>
  <?php
  }
?>
	<form name="adminForm2" action="" method="post" enctype="multipart/form-data">

<div style="height: auto; width: 1012px;" id="ext-gen37">
<div class="tab-page" id="info-page">
<table class="adminform">
	<tbody><tr> 
   		<td width="689" valign="top">
			<table width="100%" border="0">
      			<tbody><tr> 
       				<td colspan="2" align="left"><h3>Chủ đề </h3></td>
    			</tr>
    			<tr class="row1"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Tên cơ sở:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
       				  <input style="width:250px;"  name="s1" size="32" maxlength="255" type="text">      				</td>
    			</tr>
				<tr class="row0"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Địa chỉ:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
<input style="width:250px;"  name="s2" size="32" maxlength="255" type="text">     				</td>
    			</tr>
					<tr class="row1"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Email:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
    <input style="width:250px;"  name="s3" size="32" maxlength="255" type="text">	    				</td>
    			</tr>
                					<tr class="row0"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Số điện thoại:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
   <input style="width:250px;"  name="s4" size="32" maxlength="255" type="text">	    				</td>
    			</tr>
    			 <tr class="row0"> 
    					    
		      		<td valign="top" width="29%">&nbsp;</td>
		      		<td width="71%">&nbsp;</td>		    
	    			</tr>
	  			  </tbody></table>
 		</td>
  	</tr>
</tbody></table>

</div>
</div>
</form>
<?php
}// ket thuc kiem tra action insert



if($_GET['action']=='update' && $_GET['option']=='com_advertisement')
	{
		$sql ="SELECT * from `".$shop."`.`coso` where MaCS = '".$_GET['id']."'";
		$ketqua = mysql_query($sql);
		$dulieu = mysql_fetch_array($ketqua);
	if(isset($_POST['ts1'])){ 
	$error="";
	if($_POST['ts1']=="" || $_POST['ts2']=="" || $_POST['ts3']=="" || $_POST['ts4']==""){
	  $error="Vui lòng nhập đầy đủ các field.";
    }else{  
         $sql = "UPDATE `".$shop."`.`coso` SET  `TenCS` = '".$_POST['ts1']."',`DiaChiCS` = '".$_POST['ts2']."',`Email` = '".$_POST['ts3']."',`SDT` = '".$_POST['ts4']."' WHERE `coso`.`MaCS` = '".$_GET['id']."'";
		 $update = mysql_query($sql) or die($sql);
         echo"<script language=javascript>window.location='index.php?option=com_advertisement&action=view';</script>";
	}
}
	?>
<div id="menu">
	<div style="float: right;">
	<div class="icon1">
<a  class="toolbar" href="?option=com_advertisement&action=view"><img src="images/toolbar/icon-32-cancel.png" alt="Remove"><br><span>Cancel</span></a>
	</div>
	</div>
	
	<div style="float: right;">
	<div class="icon1">
<a class="toolbar" onclick="adminForm3.submit();"><img src="images/toolbar/icon-32-save.png"  alt="New"><br><span>Save</span></a>
	</div>
	</div>
</div>

<div class="header" style="background: url(images/dashboard/icon-48-section.png)  no-repeat scroll 0% 0% transparent; text-indent: 30px; line-height: 50px;">
  <h2 style="margin: 0px;">&nbsp;&nbsp;&nbsp;&nbsp;Cơ sở: [ Sửa ]</h2>
</div><br /><br />
<?php
	if($error!=""){
	?>
	 <div class="error" align="center"><img src='../images/cancel_f2.png' width='10px' height='10px'/> <?php echo $error ?></div>
  <?php
  }
?>
		<form name="adminForm3" action="" method="post" enctype="multipart/form-data">

<div style="height: auto; width: 1012px;" id="ext-gen37">
<div class="tab-page" id="info-page">
<table class="adminform">
	<tbody><tr> 
   		<td width="689" valign="top">
			<table width="100%" border="0">
      			<tbody><tr> 
       				<td colspan="2" align="left"><h3>Chủ đề </h3></td>
    			</tr>
    			<tr class="row1"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Chủ đề:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
       				  <input style="width:250px; height:20px"  name="ts1" size="32" maxlength="255" type="text" value="<?php echo $dulieu['TenCS'] ?>">      				</td>
    			</tr>
				<tr class="row0"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Địa chỉ:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
        <input style="width:250px; height:20px"  name="ts2" size="32" maxlength="255" type="text" value="<?php echo $dulieu['DiaChiCS'] ?>">     				</td>
    			</tr>
					<tr class="row1"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Email:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
 <input style="width:250px; height:20px"  name="ts3" size="32" maxlength="255" type="text" value="<?php echo $dulieu['Email'] ?>">  	    				</td>
    			</tr>
                					<tr class="row0"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Số điện thoại:</div>      				</td>
      				<td width="71%" style="padding-left:15px"> 
 <input style="width:250px; height:20px"  name="ts4" size="32" maxlength="255" type="text" value="<?php echo $dulieu['SDT'] ?>">  	    				</td>
    			</tr>
				<tr class="row1"> 
      				<td width="29%"><div style="text-align: right; font-weight: bold;">Vị trí:</div>      				</td>
      				<td width="71%" style="padding-left:15px"><?php echo $dulieu['ViTri'];?></td>
    			</tr>
    			 <tr class="row0"> 
    					    
		      		<td valign="top" width="29%">&nbsp;</td>
		      		<td width="71%">&nbsp;</td>		    
	    			</tr>
	  			  </tbody></table>
 		</td>
  	</tr>
</tbody></table>

</div>
</div>
</form>
<?php
}//ket thuc update


if($_GET['action']=='update_dx' && $_GET['option']=='com_advertisement')
{

        $sql ="SELECT * from `".$shop."`.`coso` WHERE `MaCS` = '".$_GET['id']."'";
		$ketqua = mysql_query($sql);
		$dulieu = mysql_fetch_array($ketqua);		
        $xuong=$dulieu['ViTri']+1;
		
		$sql1 ="SELECT * from `".$shop."`.`coso` WHERE ViTri='".$xuong."'";
		$ketqua1 = mysql_query($sql1);
		$dulieu1 = mysql_fetch_array($ketqua1);
  
		
		$sql2 = "UPDATE `".$shop."`.`coso` SET  `ViTri` = '".$xuong."'  WHERE `MaCS` = '".$_GET['id']."'";
		$update2 = mysql_query($sql2);
		
        $sql3 = "UPDATE `".$shop."`.`coso` SET  `ViTri` = '".$dulieu['ViTri']."' WHERE `MaCS` = '".$dulieu1['MaCS']."'";
		$update3 = mysql_query($sql3);
	   echo"<script language=javascript>window.location='index.php?option=com_advertisement&action=view';</script>";
 
}

if($_GET['action']=='update_dl' && $_GET['option']=='com_advertisement')
{

        $sql ="SELECT * from `".$shop."`.`coso` WHERE `MaCS` = '".$_GET['id']."'";
		$ketqua = mysql_query($sql);
		$dulieu = mysql_fetch_array($ketqua);		
        $xuong=$dulieu['ViTri']-1;
		
		$sql1 ="SELECT * from `".$shop."`.`coso` WHERE  ViTri='".$xuong."'";
		$ketqua1 = mysql_query($sql1);
		$dulieu1 = mysql_fetch_array($ketqua1);
  
		
		$sql2 = "UPDATE `".$shop."`.`coso` SET  `ViTri` = '".$xuong."'  WHERE `MaCS` = '".$_GET['id']."'";
		$update2 = mysql_query($sql2);
		
        $sql3 = "UPDATE `".$shop."`.`coso` SET  `ViTri` = '".$dulieu['ViTri']."' WHERE `MaCS` = '".$dulieu1['MaCS']."'";
		$update3 = mysql_query($sql3);
	   echo"<script language=javascript>window.location='index.php?option=com_advertisement&action=view';</script>";
 
}
?>


</div>
